import requests
# 禁用 SSL 警告
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class ArgoClient(object):

    def __init__(self, host, port, user_name, password):
        self.host = host
        self.port = port
        self.user_name = user_name
        self.password = password
        self.session = requests.Session()
        self.login()

    def login(self):
        url = f'https://{self.host}:{self.port}/api/v1/session'
        data = {'username': self.user_name, 'password': self.password}
        # print(url)
        self.session.post(url, json=data, verify=False)
        # print(response.text)

    def action_restart(self, project, app_name, namespace):
        url = f'https://{self.host}:{self.port}/api/v1/applications/{project}-{app_name}/resource/actions'
        url_params = {
            'appNamespace': 'argocd',
            'namespace': namespace,
            'resourceName': app_name,
            'version': 'v1',
            'kind': 'Deployment',
            'group': 'apps'
        }
        # headers = {
        #     'Cookie': f'argocd.token={self.cookie_value}'
        # }
        response = self.session.post(
            url, params=url_params, json='restart', verify=False)
        # print(response.text)
        # print(response.url)
        # print(response.ok)
        return response.ok


class BatchRestart(object):

    def __init__(self, node_info: dict):
        self.node_info = node_info

    def run(self):
        for node_ip, node_apps in self.node_info.items():
            node_port = node_apps.get('port', 58080)
            user_name = node_apps.get('user_name', 'admin')
            password = node_apps.get('password', 'Nvx@2024')
            argo_cli = ArgoClient(node_ip, node_port, user_name, password)

            for project_ns in node_apps.get('project-ns', []):
                project, ns = project_ns.split(',')

                for app in node_apps.get('apps', []):

                    status = argo_cli.action_restart(project, app, ns)
                    if status:
                        print(
                            f'[{node_ip}] restart {app}, ns={ns}, project={project}, Success.'
                        )
                    else:
                        print(
                            f'[{node_ip}] restart {app}, ns={ns}, project={project}, Failed.'
                        )


if __name__ == '__main__':
    # argo_host = '192.168.50.45'
    # argo_port = 58080
    # argo_user_name = 'admin'
    # argo_password = 'Nvx@2024'
    #
    # argo_project = 'baize'
    # argo_app_name = 'dag-proxy'
    # argo_namespace = 'nvpc-apps-01'
    #
    # argo = ArgoClient(argo_host, argo_port, argo_user_name, argo_password)
    # argo.action_restart(argo_project, 'dag-proxy', argo_namespace)
    # argo.action_restart(argo_project, 'dag-stable-serving', argo_namespace)

    params = {
        # 政务1
        '192.168.50.45': {
            'project-ns': ['baize,nvpc-apps-01'],
            'apps': ['dag-proxy', 'dag-stable-serving', 'dag-stable-web']
        },
        # # 政务2
        # '192.168.50.63': {
        #     'project-ns': ['baize,nvpc-apps-01'],
        #     'apps': ['dag-proxy', 'dag-stable-serving', 'dag-stable-web']
        # },
        #
        # # 政务3 and 中心
        # '192.168.50.52': {
        #     'project-ns': ['edge,nvpc-edge-01', 'baize,nvpc-apps-01'],
        #     'apps': ['dag-proxy', 'dag-stable-serving', 'dag-stable-web']
        # },
        #
        # # 企业1
        # '192.168.50.35': {
        #     'project-ns': ['baize,nvpc-apps-01'],
        #     'apps': ['dag-proxy', 'dag-stable-serving', 'dag-stable-web']
        # },
        #
        # # 互联中心
        # '192.168.50.28': {
        #     'project-ns': ['baize,nvpc-apps-01'],
        #     'apps': ['dag-proxy', 'dag-stable-serving', 'dag-stable-web']
        # },
    }

    bs = BatchRestart(params)
    bs.run()
